package cn.signalpha.modules.shop.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.CollectionUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.entity.GoodsComment;
import cn.signalpha.modules.shop.entity.enums.GoodsStatusEnum;
import cn.signalpha.modules.shop.service.GoodsCommentService;
import cn.signalpha.modules.shop.service.GoodsService;
import cn.signalpha.modules.shop.vo.GoodsListVO;
import cn.signalpha.modules.shop.vo.GoodsVO;
import cn.signalpha.modules.shop.wrapper.GoodsWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;


/**
 * 商品
 *
 * @author suguangjun
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/wap/goods")
@Api(value = "商品", tags = "商品")
public class ApiGoodsController extends BaseController {

	private final GoodsService goodsService;
	private final GoodsCommentService goodsCommentService;

    @GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="name", dataType = "string", value = "名称"),
		@ApiImplicitParam(name="categoryId", dataType = "string", value = "分类id"),
		@ApiImplicitParam(name="sellerCategoryId", dataType = "string", value = "商户商品分类id"),
		@ApiImplicitParam(name="isHot", dataType = "string", value = "是否热门"),
		@ApiImplicitParam(name="isPush", dataType = "string", value = "是否推荐"),
		@ApiImplicitParam(name="sellerId", dataType = "string", value = "商户id"),
		@ApiImplicitParam(name="goodsPriceBegin", dataType = "string", value = "开始价格"),
		@ApiImplicitParam(name="goodsPriceEnd", dataType = "string", value = "结束价格"),
	})
	@ApiOperationSupport(order = 1)
    @ApiOperation("商品分页")
    public R<IPage<GoodsListVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query){
		params.put("kind", "2");
		params.put("status", String.valueOf(GoodsStatusEnum.UP.getType()));
        return R.data(goodsService.selectGoodsPage(Condition.getPage(query), params));
    }

	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name="ids", dataType = "string", value = "逗号分隔"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation("商品列表")
	public R<List<GoodsListVO>> list(@ApiIgnore @RequestParam Map<String, Object> params){
		List<Long> goodsIds = Func.toLongList(String.valueOf(params.get("ids")));
		if(CollectionUtil.isEmpty(goodsIds)){
			throw new ServiceException("ids不能为空");
		}
		params.put("status", String.valueOf(GoodsStatusEnum.UP.getType()));
		List<GoodsListVO> goodsListList = goodsService.selectGoodsList(params);
		goodsListList.sort(((o1, o2) -> {
			int io1 = goodsIds.indexOf(o1.getId());
			int io2 = goodsIds.indexOf(o2.getId());
			if (io1 != -1) {
				io1 = goodsListList.size() - io1;
			}
			if (io2 != -1) {
				io2 = goodsListList.size() - io2;
			}
			return io2 - io1;
		}));
		return R.data(goodsListList);
	}

	@GetMapping("/info")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "商品详情", notes = "")
	public R<GoodsVO> info(@RequestParam Long id){
		return R.data(GoodsWrapper.build().entityVO(goodsService.getById(id), getUser().getUserId()));
	}

	@GetMapping("/comment/page")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "商品评论列表", notes = "")
	public R<IPage<GoodsComment>> commentPage(@RequestParam String goodsId, Query query){
		IPage<GoodsComment> page = goodsCommentService.page(Condition.getPage(query), Wrappers.<GoodsComment>query().lambda()
			.eq(GoodsComment::getGoodsId, goodsId).eq(GoodsComment::getIsShow, 1).orderByDesc(GoodsComment::getId)
		);
		return R.data(page);
	}

}
